package com.mark.service.impl;

import com.mark.common.ServerResponse;
import com.mark.dao.OrderMapper;
import com.mark.dao.ProductMapper;
import com.mark.dao.UserMapper;
import com.mark.enums.OrderStatusEnum;
import com.mark.enums.ProductStatusEnum;
import com.mark.enums.UserStateEnum;
import com.mark.exception.StatisticException;
import com.mark.service.IStatisticService;
import com.mark.vo.StatisticVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by Choisaaaa on 2018/5/23.
 * 数据统计的业务处理
 */

@Service("iStatisticService")
public class StatisticServiceImpl implements IStatisticService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private ProductMapper productMapper;

    @Override
    public ServerResponse getCampusData() {
        try {
            //所有用户
            int userCount = userMapper.selectCount(null);
            //卖家
            int sellerCount = userMapper.selectCount(UserStateEnum.SELLER_CUSTOMER.getRole());
            //普通用户
            int commonUserCount = userMapper.selectCount(UserStateEnum.ROLE_CUSTOMER.getRole());
            //所有商品
            int productCount = productMapper.selectCountByStatus(null, null);
            //在售商品
            int productSaleCount = productMapper.selectCountByStatus(ProductStatusEnum.ON_SALE.getStatus(), null);
            //订单总数
            int orderCount = orderMapper.selectCount(null);
            //交易完成的订单
            int orderFinish = orderMapper.selectCount(OrderStatusEnum.ORDER_SUCCESS.getCode());

            StatisticVo statisticVo = new StatisticVo();
            statisticVo.setUserCount(userCount);
            statisticVo.setSellerCount(sellerCount);
            statisticVo.setCommonUserCount(commonUserCount);
            statisticVo.setProductCount(productCount);
            statisticVo.setProductSaleCount(productSaleCount);
            statisticVo.setOrderCount(orderCount);
            statisticVo.setOrderFinish(orderFinish);
            return ServerResponse.createBySuccess(statisticVo);
        }catch (Exception e){
            throw new StatisticException("getCampusData",e.getClass()+e.getMessage());
        }
    }
}
